package com.example.demo.grpc.service;

import com.example.demo.Request;
import com.example.demo.Response;
import com.example.demo.UserServiceGrpc;
import com.example.demo.grpc.utils.MsgBuilder;
import com.example.demo.grpc.utils.MsgParse;
import com.example.demo.user.UserReq;
import com.example.demo.user.UserRes;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * @author ls
 * @description
 * @date 2025/6/6 14:21
 */
@GrpcService
@Slf4j
public class UserSeriveGrpc extends UserServiceGrpc.UserServiceImplBase {
    @Override
    public void test(Request request, StreamObserver<Response> responseObserver) {
        try {
            UserReq data = MsgParse.parseReqBody(request);
            log.info("receive request: {}", data.getUserName());
            UserRes helloResponse = UserRes.newBuilder().setUserName("name").setAge("18").setRealName("张三")
                    .build();
            responseObserver.onNext(MsgBuilder.success(helloResponse));
        } catch (InvalidProtocolBufferException e) {
            log.error("parse error", e);
            responseObserver.onNext(MsgBuilder.fail("500", "parse error"));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        responseObserver.onCompleted();
    }
}
